package com.unproy.model.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "TBARUP")
public class ArticleUpdate implements Serializable, Comparable<ArticleUpdate>, Publishable {
	private static final long	serialVersionUID	= 9069763023008108697L;
	private Long				articleUpdateId;
	private Article				article;
	private ArticleUpdate		parentArticleUpdate;
	private ArticleUpdate		childArticleUpdate;
	private User				createdBy;
	private Date				createdOn			= new Date();
	private Boolean				approved			= false;
	private User				approvedBy;
	private Date				approvedOn;
	private User				publishedBy;
	private Date				publishedOn;
	private String				title;
	private String				subTitle;
	private String				text;
	
	@Id
	@SequenceGenerator(name = "articleUpdateId", sequenceName = "SQARUP")
	@GeneratedValue(generator = "articleUpdateId", strategy = GenerationType.SEQUENCE)
	@Column(name = "ARUPID", unique = true, nullable = false)
	public Long getArticleUpdateId() {
		return this.articleUpdateId;
	}
	
	public void setArticleUpdateId(Long articleUpdateId) {
		this.articleUpdateId = articleUpdateId;
	}
	
	@OneToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "ARTCID", nullable = false)
	public Article getArticle() {
		return this.article;
	}
	
	public void setArticle(Article article) {
		this.article = article;
	}
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PUPDID")
	public ArticleUpdate getParentArticleUpdate() {
		return this.parentArticleUpdate;
	}
	
	public void setParentArticleUpdate(ArticleUpdate parentArticleUpdate) {
		this.parentArticleUpdate = parentArticleUpdate;
	}
	
	@OneToOne(fetch = FetchType.LAZY, mappedBy = "parentArticleUpdate")
	public ArticleUpdate getChildArticleUpdate() {
		return this.childArticleUpdate;
	}
	
	public void setChildArticleUpdate(ArticleUpdate childArticleUpdate) {
		this.childArticleUpdate = childArticleUpdate;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CUSRID", nullable = false)
	@Override
	public User getCreatedBy() {
		return this.createdBy;
	}
	
	@Override
	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}
	
	@Column(name = "CRTDON", nullable = false)
	@Override
	public Date getCreatedOn() {
		return this.createdOn;
	}
	
	@Override
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}
	
	@Column(name = "ISAPRV", nullable = false)
	@Override
	public Boolean isApproved() {
		return this.approved;
	}
	
	@Override
	public void setApproved(Boolean approved) {
		this.approved = approved;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "AUSRID")
	@Override
	public User getApprovedBy() {
		return this.approvedBy;
	}
	
	@Override
	public void setApprovedBy(User approvedBy) {
		this.approvedBy = approvedBy;
	}
	
	@Column(name = "APRVON")
	@Override
	public Date getApprovedOn() {
		return this.approvedOn;
	}
	
	@Override
	public void setApprovedOn(Date approvedOn) {
		this.approvedOn = approvedOn;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PUSRID")
	@Override
	public User getPublishedBy() {
		return this.publishedBy;
	}
	
	@Override
	public void setPublishedBy(User publisedBy) {
		this.publishedBy = publisedBy;
	}
	
	@Column(name = "PBSHON")
	@Override
	public Date getPublishedOn() {
		return this.publishedOn;
	}
	
	@Override
	public void setPublishedOn(Date publishedOn) {
		this.publishedOn = publishedOn;
	}
	
	@Column(name = "ATITLE", nullable = false)
	public String getTitle() {
		return this.title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	@Column(name = "SUBTIT")
	public String getSubTitle() {
		return this.subTitle;
	}
	
	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}
	
	@Lob
	@Column(name = "ARTTXT", nullable = false)
	public String getText() {
		return this.text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.articleUpdateId == null) ? 0 : this.articleUpdateId.hashCode());
		result = prime * result + ((this.createdBy == null) ? 0 : this.createdBy.hashCode());
		result = prime * result + ((this.createdOn == null) ? 0 : this.createdOn.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArticleUpdate other = (ArticleUpdate) obj;
		if (this.articleUpdateId == null) {
			if (other.articleUpdateId != null)
				return false;
		} else if (!this.articleUpdateId.equals(other.articleUpdateId))
			return false;
		if (this.createdBy == null) {
			if (other.createdBy != null)
				return false;
		} else if (!this.createdBy.equals(other.createdBy))
			return false;
		if (this.createdOn == null) {
			if (other.createdOn != null)
				return false;
		} else if (!this.createdOn.equals(other.createdOn))
			return false;
		return true;
	}
	
	@Override
	public int compareTo(ArticleUpdate articleUpdate) {
		return getCreatedOn().compareTo(articleUpdate.getCreatedOn());
	}
	
	@Override
	public String toString() {
		return getTitle();
	}
}
